package com.zeyi.chat.controller;

import com.zeyi.chat.dto.Chat;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.utils.SocketUtil;
import com.zeyi.chat.manager.UserManager;
import com.zeyi.chat.views.ChatContentListItem;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;

/**
 * @author zeyi
 */
public class ChatController {

    @FXML
    private ListView<Node> chatContentList;

    @FXML
    private TextArea messageArea;

    /**
     * 发送聊天信息
     */
    public void sendMessage(ActionEvent actionEvent) {
        String text = messageArea.getText();
        if (text.equals("")) {
            return;
        }
        messageArea.setText("");
        Chat receiver = UserManager.getChat();
        Pane right = new ChatContentListItem().Me(UserManager.getCurrentUser().getAvatar(), text, receiver.getCreateId() == null ? "0":"1");
        chatContentList.getItems().add(right);
        Message message = null;
        if (receiver.getCreateId() == null) {
            message = new Message("ChatHandler/chatPrivate", "ChatHandler/chatPrivate", UserManager.getCurrentUser(), receiver, text);
        } else {
            message = new Message("ChatHandler/chatGroup", "ChatHandler/chatGroup", UserManager.getCurrentUser(), receiver, text);
        }
        SocketUtil.send(message);
    }
}
